<? 
	include('base.php');

	User::protect();

	include_class('links');

	$section = 'links';
	$page_title = 'View/Edit Links';

	$category_id = ($_GET['category_id'] > 0 && is_numeric($_GET['category_id'])) ? $_GET['category_id'] : 0;

	$lc = LinkCategory::get($category_id);

	if (!db::isError($l)) { 
		switch($_GET['task']) {
			case 'deactivate':
				$res = $lc->deactivate();
				if (!db::isError($res)) {
					header('Location: links.php?category_id=' . $category_id);
				}
				break;
			case 'activate':
				$res = $lc->activate();
				if (!db::isError($res)) {
					header('Location: links.php?category_id=' . $category_id);
				}
				break;
			case 'delete':
				$res = $lc->remove();
				if (!db::isError($res)) {
					header('Location: links.php');
				}
				break;
		}
	}

	include('layout/header.php');
?>

<? if (db::isError($ma)) { ?>

        <div id="breadcrumb">
	        <a href="index.php">Audition &#62;</a>
                Links
        </div>

	<? if (db::isError($ll)) {
		$ll->outputList();
	} ?>
	
<? } else if ($category_id == 0) { ?>

        <div id="breadcrumb">
	        <a href="index.php">Audition &#62;</a>
                Links
        </div>

        <h1>categories:</h1>
        <div class="inset">
        <? if (User::isAdmin()) { ?>
                <a href="links_category_add.php">Add Category</a>
        <? } ?>

	<p>
	<table class="list" border="0" cellspacing="1" cellpadding="0">
	<tr>
		<td class="header">Name</td>
	</tr>
	<?
        $categories = LinkCategory::getList(0);

	if (is_array($categories)) { 
		foreach ($categories as $c) { ?>
		<tr>
			<td valign="top">
				<a href="links.php?category_id=<?= $c->getID() ?>"><?= $c->getName() ?></a>
        			<? if (!$c->isActive()) { ?>
					<img src="images/inactive.gif" alt="Entry is Inactive" width="10" height="10" title="Entry is Inactive">
				<? } ?>
			</td>
		</tr>
		<? }
	}
	
	if (count($categories) == 0 || db::isError($categories)) { ?>
		<tr>
			<td colspan="2"><strong>No Categories</strong></td>
		</tr>
	<? } ?>
	</table>
	</p>		
	</div>

<? } else { ?>

	<div id="breadcrumb">
		<a href="index.php">Audition &#62;</a>
                <a href="links.php">Links &#62;</a>

		<? $breadcrumb = $lc->getCategoryTrail();
			if ($breadcrumb) { 
				foreach($breadcrumb as $lcBC) {
					if ($lcBC->getID() == $lc->getID()) { 
						echo($lcBC->getName());
					}
					else {
						echo('<a href="links.php?category_id=' . $lcBC->getID() . '">' . $lcBC->getName() . ' &#62;</a> ');
					}
				}
			}
		 if ($lc->error) { ?>
			<br><span class="error">
				<?= $lc->error ?>
			</span>
		<? } ?>
	</div>
		
	<h1>category:</h1>
	<div class="inset">
	<h2><?= $lc->getName() ?></h2>
	<span class="small">
		Status: <strong><? if ($lc->isActive()) { ?>Active<? } else { ?>Inactive<? } ?></strong>
	</span>
	<p>
	<?= $lc->getDescription() ?>
	</p>
	<? if (User::isAdmin()) { ?>
		<input class="button" type="button" value="Edit" name="" onclick="location.href='links_category_edit.php?category_id=<?= $category_id ?>'; return false">
	
		<? if ($lc->isActive()) { ?>
			<input class="button" type="button" value="Deactivate" name="" onclick="location.href='links.php?category_id=<?= $category_id ?>&task=deactivate'; return false">
		<? } else { ?>
			<input class="button" type="button" value="Activate" name="" onclick="location.href='links.php?category_id=<?= $category_id ?>&task=activate'; return false">
		<? } ?>
		<input class="button" type="button" value="Remove" name="" onclick="if (confirm('This will remove the category, and all links inside it. Continue?')) { location.href = '<?= $_SERVER['PHP_SELF'] ?>?task=delete&category_id=<?= $category_id ?>';}">
	<? } ?>
	</div>
		
	<h1>links:</h1>
	<div class="inset">
	<p>
	<? if (User::isAdmin()) { ?>
		<a href="link_add.php?category_id=<?= $category_id ?>">Add Link</a>
	<? } ?>
	</p>
	<p>
	<table class="list" border="0" cellspacing="1" cellpadding="0">
	<tr>
		<td class="header">Name</td>
		<td class="header">URL</td>
	</tr>
	<?
	$ll = Link::getList($category_id);

	if (is_array($ll)) { 
		foreach ($ll as $l) { ?>
		<tr>
			<td valign="top"><a href="link_edit.php?category_id=<?= $category_id ?>&id=<?= $l->getID() ?>"><?= $l->getName() ?></a></td>
			<td valign="top"><?= $l->getURL() ?><? if (!$l->isActive()) { ?> <img src="images/inactive.gif" alt="Entry is Inactive" width="10" height="10" title="Entry is Inactive"><? } ?></td>
		</tr>				
		<? }
	}
	
	if (count($ll) == 0 || db::isError($ll)) { ?>
		<tr>
			<td colspan="2"><strong>No Links</strong></td>
		</tr>
	<? } ?>
	</table>
	</p>		
	</div>

<? } ?>

<? include('layout/footer.php'); ?>
